﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Buy2Site.UserControls
{
    public partial class PublishAdControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void PublishAd_click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                using (Buy2SiteDBEntities ent = new Buy2SiteDBEntities())
                {
                    User curret=null;
                    int state = 1; // 1 = product cannot be modified, 0 = can
                    foreach (var item in ent.User)
                    {
                        if (item.UserName == ((User)Session["User"]).UserName)
                        {
                            curret = item;
                            break;
                        }
                    }
                    Product product = new Product()
                    {
                        Title=txtTitle.Text,                        
                        ShortDescription=txtShortDescription.Text,
                        LongDescription=txtLongDescription.Text,
                        Price=Convert.ToDecimal(txtPrice.Text),
                        Date=DateTime.Now,
                        State=state,
                        Picture1=FileUploadImage1.FileBytes,
                        Picture2=FileUploadImage2.FileBytes,
                        Picture3=FileUploadImage3.FileBytes,
                        UserID=curret.ID
                    };
                    ent.Product.AddObject(product);
                    ent.SaveChanges();
                    lblSuccess.Text = "New product was successfully published";
                    linkToMain.NavigateUrl = "~/Pages/HomePage.aspx";
                    linkToMain.Text = "Go to Home page";
                }
            }
        }
    }
}